//
//  PicCollectionCell.swift
//  WBSwift
//
//  Created by maker on 2016/11/24.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit
import SDWebImage

protocol PhotoBrowerCellDelegate : NSObjectProtocol {
    func imageClick()
}

class BrowerCollectionCell: UICollectionViewCell {
    var url : URL? {
        didSet{
            setupContent(url)
        }
    }
    lazy var scrollView : UIScrollView = UIScrollView()     //滚动视图
    lazy var iv : UIImageView = UIImageView()               //图片
    lazy var progress : ProgressView = ProgressView()       //进度条
    var delegate : PhotoBrowerCellDelegate?                 //图片 cell 代理
    

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
//MARK:==========设置 UI==========
extension BrowerCollectionCell{
    func setupUI() {
        //1.添加控件
        addSubview(scrollView)
        scrollView.addSubview(iv)
        addSubview(progress)
        
        //2.设置布局
        scrollView.frame = contentView.bounds
        scrollView.frame.size.width -= 20
        progress.bounds = CGRect(x: 0, y: 0, width: 80, height: 80)
        progress.center = CGPoint(x: screenWidth * 0.5, y: screenHeight * 0.5)
        progress.isHidden = true
        progress.backgroundColor = UIColor.clear
        
        //3.添加事件
        iv.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action:#selector(BrowerCollectionCell.imageClick))
        iv.addGestureRecognizer(tap)
        
        
    }
}

//MARK:==========设置 内容==========
extension BrowerCollectionCell{
    func setupContent(_ smallUrl : URL?) {
        guard let url = smallUrl else {
            return
        }
        //取出图片
        let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: url.absoluteString)
        
        let imageH = screenWidth/(image?.size.width)! * (image?.size.height)!
        
        var y : CGFloat = 0
        
        if imageH >= screenHeight {
            y = 0
        }else{
            y = (screenHeight - imageH)/2
        }
        iv.frame = CGRect(x: 0, y: y, width: scrollView.widths(), height: imageH)
        progress.isHidden = false
        iv.sd_setImage(with: getBigUrl(url), placeholderImage: image, options: [], progress: { (current, total) in
                self.progress.progress = CGFloat(current)/CGFloat(total)
            }) { (_, _, _, _) in
                
               self.progress.isHidden = true
        }
        scrollView.contentSize = CGSize(width: screenWidth, height: imageH)
        
    }
    func getBigUrl(_ smallUrl:URL) -> URL {
        let smallString = smallUrl.absoluteString
        let bigString = smallString.replacingOccurrences(of: "thumbnail", with: "bmiddle")
        return URL(string: bigString)!
    }
}

//MARK:==========点击事件==========
extension BrowerCollectionCell{
    func imageClick() {
        delegate?.imageClick()
    }
}






